package server

import (
	"sync"
)

type Servers struct {
	mutex sync.Mutex
	nodes map[string]*ServerNode
}

func NewServerList() Servers {
	return Servers{
		nodes: make(map[string]*ServerNode, 100),
	}
}

func (s *Servers) Add(id string, node *ServerNode) {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	s.nodes[id] = node
}
func (s *Servers) Delete(id string) {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	_, ok := s.nodes[id]
	if ok {
		delete(s.nodes, id)
	}
}

func (s *Servers) Find(id string) *ServerNode {
	node, ok := s.nodes[id]
	if ok {
		return node
	}
	return nil
}

func (s *Servers) List() map[string]*ServerNode {
	s.mutex.Lock()
	defer s.mutex.Unlock()

	var all map[string]*ServerNode = make(map[string]*ServerNode)

	for key, v := range s.nodes {
		all[key] = v
	}
	return all
}
